﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using Hack.SystemAPI;
namespace Hack.Security.Hide_Idetitty
{
    /// <summary>
    /// Author: Max Black
    /// Uses a batch script that it creates to delete your executable, then deletes itself.
    /// Platform: .NET Framework 2.0
    /// </summary>
    public static class DeleteSelf
    {
        [Obsolete("use: ExitAndDeleteEXE, still using code inside for tests")]
        public static void CloseDeleteThisAssembly()
        {
            //Use this line if your running from console
            //string pa = Process.GetCurrentProcess().MainModule.FileName;

            //Use this line if your running from Windows Form

            Process.GetCurrentProcess().Exited += delegate
            {
                string pa = Application.StartupPath;
                string bf = "@echo off" + Environment.NewLine +
                            ":dele" + Environment.NewLine +
                            "del \"" + pa + "\"" + Environment.NewLine +
                            "if Exist \"" + pa + "\" GOTO dele" + Environment.NewLine +
                            "del %0";

                //string filename = RandomString(RandomNumber(5, 15)) + ".bat";
                string filename = Path.GetRandomFileName() + ".bat";

                StreamWriter file = new StreamWriter(Environment.GetEnvironmentVariable("TMP") + filename);
                file.Write(bf);
                file.Close();

                Process proc = new Process();
                proc.StartInfo.FileName = Environment.GetEnvironmentVariable("TMP") + filename;
                proc.StartInfo.CreateNoWindow = true;
                proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                proc.StartInfo.UseShellExecute = true;

                proc.Start();
                proc.PriorityClass = ProcessPriorityClass.Normal;
            };
            Process.GetCurrentProcess().Kill();
            
        }
        public static void ExitAndDeleteEXE(Action doBeforeExit)
        {
            doBeforeExit();
            Process.Start("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3032 > Nul & Del " + Application.ExecutablePath);
            Process.GetCurrentProcess().Kill();
            //Application.Exit();
            //Environment.Exit(0);
            //Process proc = new Process();
            //proc.StartInfo.CreateNoWindow = true;
            //proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //proc.StartInfo.UseShellExecute = true;
            //switch (SystemInfo.GetOSVersion())
            //{
            //    case SystemInfo.Version.Windows7:
            //        proc.StartInfo.FileName = "cmd.exe";
            //        proc.StartInfo.Arguments = "/C choice /C Y /N /D Y /T 3 & Del " + Application.ExecutablePath;

            //        proc.Start();
            //        //Process.Start("cmd.exe",
            //        //              "/C choice /C Y /N /D Y /T 3 & Del " +
            //        //              Application.ExecutablePath
            //        //);

            //        Application.Exit();
            //        Environment.Exit(0);
            //        break;
            //    default:
            //        proc.StartInfo.FileName = "cmd.exe";
            //        proc.StartInfo.Arguments= "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " + Application.ExecutablePath;

            //        proc.Start();
            //        //Process.Start("cmd.exe",
            //        //    "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del " +
            //        //    Application.ExecutablePath
            //        //);

            //        Application.Exit();
            //        Environment.Exit(0);
            //        break;
            //}

            //Application.Exit();
            //Environment.Exit(0);


        }
        public static void ExitAndDeleteEXE()
        {
            ExitAndDeleteEXE(delegate { });
        }
    }
}